package mango.client;
import java.io.*;
import java.net.*;


public abstract class MangoClient {
	
	public Socket mSocket = null;
    public PrintWriter out = null;
    public BufferedReader in = null;
    private MangoListener ears = null;
    
	public void connectToServer(String host, int port) {
		try {
        	System.out.println(">#< Attempting to connect to "+host+":"+port+"...");
            mSocket = new Socket(host, port);
            out = new PrintWriter(mSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.out.println(">!< Don't know about host: "+host+".");
            System.exit(1);
        } catch (IOException e) {
            System.out.println(">!< Couldn't get I/O for the connection to: "+host+":"+port+".");
            System.exit(1);
        }
        
        ears = new MangoListener(in, this);
        
        System.out.println(">#< Connection successful.");
	}

	public void pushToServer(String command) {
		System.out.println(">#< Sending message: " + command);
		out.println(command);
	}
	
	public void quit() {
		System.exit(1);
	}

	public void disconnect() {
		try {
			ears.stop();
			out.close();
			in.close();
			mSocket.close();
		}
		catch (Exception e) {}
    }
}